package cn.wizzer.app.web.commons.ext.websocket.action.attendant;

import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.UserChatMsgService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.filter.AttendantFilter;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zl
 * @date 2020/6/5 17:32
 */
@IocBean
@NettyAction(uri = "/attendant/target/users", filters = {TokenFilter.class, AttendantFilter.class})
public class AttendantTargetUsersAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserService userService;
    @Inject
    private UserChatMsgService chatMsgService;

    @Override
    public Result action(Channel income, JSONObject params) {
        String attendantId = params.getString("appid");
        ChatUserVO attendant = NettyCache.globalGet(attendantId);
        List<User> users = new ArrayList<>();
        List<String> userIds = chatMsgService.findRecentChatUser(attendantId).stream().distinct().collect(Collectors.toList());
        if(attendant.getUsers() == null || attendant.getUsers().size() == 0){
            attendant.setUsers(userIds);
        }
        if(attendant.getUsers().size() > 0){
            Sql sql = Sqls.create("SELECT id, nickName, sign, isOnline " +
                    "FROM user $condition ");
            sql.setCondition(Cnd.where("id", "IN", attendant.getUsers()));
            List<User> userList = userService.listEntity(sql);
            if(userIds.size() > 0){
                //新接入且未发送消息的用户
                List<String> newUsers = new ArrayList<>(attendant.getUsers());
                newUsers.removeAll(userIds);

                Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, JSONObject::toJSONString));
                //region 排序
                if(newUsers.size() > 0){
                    for (String newUser : newUsers) {
                        String userStr = userMap.get(newUser);
                        if(Strings.isNotBlank(userStr)){
                            users.add(JSONObject.parseObject(userStr, User.class));
                        }
                    }
                }
                for (String userId : userIds) {
                    String userStr = userMap.get(userId);
                    if(Strings.isNotBlank(userStr)){
                        users.add(JSONObject.parseObject(userStr, User.class));
                    }
                }
                //endregion
            }else {
                users = userList;
            }
        }
        return Result.success().addData(users);
    }
}
